package com.peng.leetcode.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * KthSmallest1
 * 230. 二叉搜索树中第K小的元素
 * https://leetcode.cn/problems/kth-smallest-element-in-a-bst/
 * <p>
 * Created on 2022/11/27
 *
 * @author lupeng
 */
public class KthSmallest1 {

    List<Integer> l = new ArrayList<>();

    public int kthSmallest(TreeNode root, int k) {
        dfs(root, k);
        return l.get(k - 1);
    }

    void dfs(TreeNode root, int k) {
        if (root == null) {
            return;
        }
        dfs(root.left, k);
        l.add(root.val);
        if (l.size() == k) {
            return;
        }
        dfs(root.right, k);
    }
}
